﻿using AutoMapper;
using B.S.DataBasic.Domain.GoodInfra;
using B.S.DataBasic.Domain.RBAC;
using B.S.DataBasic.ErrorCode;
using B.S.DataBasic.Infrastructure.Implement;
using B.S.DataBasic.Read.API.Dtos;
using MediatR;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;

namespace B.S.DataBasic.Read.API.Application
{
    public class LoginCommandHandler : IRequestHandler<LoginCommand, APIResult<User>>
    {
        private readonly IBaseRepository<User> userRepository;
        private readonly IMapper mapper;
        public LoginCommandHandler(IBaseRepository<User> userRepository, IMapper mapper)
        {
            this.userRepository = userRepository;
            this.mapper = mapper;
        }

        public async Task<APIResult<User>> Handle(LoginCommand request, CancellationToken cancellationToken)
        {
            APIResult<User> result = new APIResult<User>();
            result.Code = EnumCode.登录成功;
            result.Message = EnumCode.登录成功.ToString();

            var list=userRepository.GetAll().Where(x=>x.UserName==request.UserName);
            if(list.Count()!=1)
            {
                result.Code = EnumCode.登录失败;
                result.Message = EnumCode.登录失败.ToString();
                return await Task.FromResult(result);
            }
            var entity = list.FirstOrDefault();
            if(entity.Password!=request.Password)
            {
                result.Code = EnumCode.登录失败;
                result.Message = EnumCode.登录失败.ToString();
                return await Task.FromResult(result);
            }
             
            
            result.Data = entity;

            return await Task.FromResult(result);
        }
    }
}
